package com.pan.controller;

import com.pan.entity.Animal;
import com.pan.service.AnimalService;
import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URI;
import java.util.Enumeration;
import java.util.Map;

@Controller
@RequestMapping(path = "/animal")
public class AnimalController {

    @Autowired
    private AnimalService animalService;

    @PostMapping(path = "/save")
    public Animal save(@RequestBody Animal animal){
       return animalService.save(animal);
    }

    @GetMapping(path = "/findbyId")
    @ResponseBody
    public Animal findById(@RequestParam String path){
        return animalService.findById(path);
    }

    @GetMapping(path = "/http")
    @ResponseBody
    public Animal useHttp(@RequestParam String id){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest httpRequest = attributes.getRequest();
        HttpServletResponse response = attributes.getResponse();
        String uri = httpRequest.getRequestURI();
        Map<String, String[]> parameterMap = httpRequest.getParameterMap();
        System.out.println(uri);
        Enumeration<String> headerNames = httpRequest.getHeaderNames();
        while(headerNames.hasMoreElements()){
            System.out.println(headerNames.nextElement());
        }
        return animalService.findById(id);
    }

}
